package cn.edu.sysu.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.sysu.dao.UserDao;
import cn.edu.sysu.dto.UserDto;
import cn.edu.sysu.util.Transaction;
import cn.edu.sysu.util.TransactionRead;

public class UserService {

	UserDao uDao = UserDao.INSTANCE;

	Transaction read = TransactionRead.INSTANCE;

	Map<Integer, UserDto> userMap;

	public List<UserDto> getUserList() throws SQLException{
		List<UserDto> list = uDao.getAllUsers(read);
		if(userMap==null){
			userMap = new HashMap<Integer, UserDto>();

			for(int i=0; i<list.size();i++ ){
				userMap.put(list.get(i).getUser_id(), list.get(i));
			}
		}

		return list;
	}

	public Map<Integer,UserDto> getUserMap() throws SQLException{
		if(userMap==null){
			this.getUserList();
		}

		return userMap;
	}



	public UserDto getUserById(int user_id) throws SQLException{
		if(userMap == null){
			this.getUserList();
		}

		return this.userMap.get(user_id);

	}
}
